﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JohnnySins.Entities;
using Dapper;

namespace JohnnySins.DAL.Repositories
{
    public class BannerRepository : BaseRepository<Banner>
    {
        public IEnumerable<Banner> FindAll(string pageName)
        {
            return Connection.Query<Banner>(@"SELECT rs.BannerId, rs.BannerFileName, rs.IsActive, rs.PageName
                    FROM [Banner] (nolock) rs where rs.PageName = @PageName", new { PageName = pageName });
        }

        public IEnumerable<Banner> FindActive(string pageName)
        {
            return Connection.Query<Banner>(@"SELECT rs.BannerId, rs.BannerFileName, rs.IsActive, rs.PageName
                    FROM [Banner] (nolock) rs where (rs.IsActive = 1) and (rs.PageName = @PageName)", new { PageName = pageName });
        }

        public override Banner Find(int id)
        {
            return Connection.Query<Banner>(
                @"select BannerId, BannerFileName, IsActive, PageName 
                from [Banner]
                where BannerId = @BannerId", new { BannerId = id }).SingleOrDefault();
        }

        public override void Insert(Banner banner)
        {
            int result = Connection.Query<int>(
                @"INSERT INTO [Banner] (BannerFileName, IsActive, PageName) 
                VALUES (@BannerFileName, @IsActive, @PageName); SELECT CAST(SCOPE_IDENTITY() AS INT);", banner).Single();
            banner.BannerId = result;
        }
        
        public void Activate(int id)
        {
            Connection.Execute(
                @"update [Banner] set IsActive = 1
                where BannerId = @BannerId", new { BannerId = id });
        }

        public void Deactivate(int id)
        {
            Connection.Execute(
                @"update [Banner] set IsActive = 0
                where BannerId = @BannerId", new { BannerId = id });
        }

        public override void Delete(int id)
        {
            Connection.Execute(
                @"delete from [Banner]
                    where BannerId = @BannerId", new { BannerId = id });
        }
    }
}
